####################################################################
## author:    Robert A. Huber, Lukas P. Fesenfeld & Thomas Bernauer
## contact:   robert.huber@ir.gess.ethz.ch
## file name: erc_popcli_factors.R
## Context:   ERC Populism Project on Public Support for Climate Politics
## started:   2018-02-12
## Summary:   Extract Factors
##############################################

# Generate Factors

#Populism

fut_vars <- paste0("pop", 1:3, "_num")
nScree(df[complete.cases(df),fut_vars]) # majority suggest 3
plotnScree(nScree(df[complete.cases(df),fut_vars]))

fac_fit_pop <- principal(df[, fut_vars], nfactors=1, rotate="oblimin")

df$peop <- fac_fit_pop$scores[,1]
df$peop_add3 <- (df$pop1_num + df$pop2_num + df$pop3_num)/3


fut_vars <- paste0("pop", 4:6, "_num")
nScree(df[complete.cases(df),fut_vars]) # majority suggest 3
plotnScree(nScree(df[complete.cases(df),fut_vars]))

fac_fit_pop <- principal(df[, fut_vars], nfactors=1, rotate="oblimin")

df$anti <- fac_fit_pop$scores[,1]
df$anti_add3 <- (df$pop4_num + df$pop5_num + df$pop6_num)/3


fut_vars <- paste0("pop", 7:9, "_num")
nScree(df[complete.cases(df),fut_vars]) # majority suggest 3
plotnScree(nScree(df[complete.cases(df),fut_vars]))

fac_fit_pop <- principal(df[, fut_vars], nfactors=1, rotate="oblimin")

df$man <- fac_fit_pop$scores[,1]
df$man_add3 <- (df$pop7_num + df$pop8_num + df$pop9_num)/3

df$pop <- sqrt((df$anti + abs(min(df$anti, na.rm = T)))*(df$peop + abs(min(df$peop, na.rm = T)))*(df$man + abs(min(df$man, na.rm = T))))
df$pop <- df$pop - mean(df$pop, na.rm = T)
df$pop <- ifelse(df$trt == "Total Control", NA, df$pop)

#WTP

fut_vars <- paste0("wtp", 1:5, "_num")
nScree(df[complete.cases(df),fut_vars]) # majority suggest 1
plotnScree(nScree(df[complete.cases(df),fut_vars]))

fac_fit_wtp <- principal(df[, fut_vars], nfactors=1, rotate="varimax")

df$wtp <- fac_fit_wtp$scores[,1]

#Climate Concern

fut_vars <- paste0("cliConcern", 1:3, "_num")
nScree(df[complete.cases(df),fut_vars]) # majority suggest 1
plotnScree(nScree(df[complete.cases(df),fut_vars]))

fac_fit_cliCon <- principal(df[, fut_vars], nfactors=1, rotate="varimax")

df$climConcern <- fac_fit_cliCon$scores[,1]

save(df, file = paste0("./final data/data_clean_", Sys.Date(), ".RData", sep=""))
write.csv(df, file = paste0("./final data/data_clean_", Sys.Date(), ".csv", sep=""))
write.dta(df, file = paste0("./final data/data_clean_", Sys.Date(), ".dta", sep=""))

rm(list=setdiff(ls(), c("df")))
